** This do-file extracts a list of parliamentary elections from V-Dem

use "$project_path/data/1_input/elections/V-Dem/vdem_cd_extract.dta", clear

keep country_name year historical_date v2eltype_*

// Keeping only the last round of parliamentary elections 
//	-> For election of the lower (or only) chamber (v2eltype = 0 if first round, = 1 if second round)
//	-> For election of the constituent assembly (v2eltype = 4 if first round, = 5 if second round)
gen round = 1 if (v2eltype_0==1 | v2eltype_4==1)
replace round = 2 if (v2eltype_1==1 | v2eltype_5==1)
drop if round==.
gen flag_constituent = 1 if v2eltype_4==1 | v2eltype_5==1

sort country_name historical_date
by country_name : gen nextround = round[_n+1]
drop if round==1 & nextround==2

// Two-round election flag
gen flag_two_round = 1 if round==2
drop v2eltype* round nextround

//Keeping only elections between 1946 and 2018
ren year Year
keep if Year>1945
keep if Year<=2018

// Standaradizing country names
ren country_name Country
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/West Bank"
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Myanmar"  if Country=="Burma/Myanmar"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Congo (DRC)"  if Country=="Democratic Republic of the Congo"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="PRK"  if Country=="North Korea"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Congo"  if Country=="Republic of the Congo"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Gambia"  if Country=="The Gambia"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States of America"
drop if Country=="Hong Kong"
drop if Country=="Republic of Vietnam"
drop if Country=="Zanzibar"

// Month variable
gen Month = month(historical_date)
drop historical_date
sort Country Year Month
duplicates drop
bys Country Year : gen temp=_n
bys Country Year : egen temp2 = max(temp)
replace Month = . if temp2==1
drop temp*

// Manual adjustments
replace Month = 4 if Country=="Iceland" & Year==1991
replace Month = 2 if Country=="Nicaragua" & Year==1947
drop if Country=="Korea, Republic of" & Year==1948 & flag_constituent==.
replace Month = . if Country=="Korea, Republic of" & Year==1948
replace Year = 1951 if Country=="Algeria" & Year==1952
replace Year = 1954 if Country=="Algeria" & Year==1955
replace Year = 1952 if Country=="India" & Year==1951
replace Year = 1947 if Country=="Ivory Coast" & Year==1946
replace Year = 1952 if Country=="Myanmar" & Year==1951
replace Year = 2012 if Country=="Egypt" & Year==2011
drop if Country=="Cameroon" & (Year==1954 | Year==1957 | Year==1959 | Year==1961)
drop if Country=="Serbia" & Year==1992 & Month==6
replace Country="Serbia and Montenegro" if Country=="Serbia" & Year==1996
drop if Country=="New Zealand" & Year==1949 & Month==9
replace Month = . if Country=="New Zealand" & Year==1949
replace Month = 6 if Country=="Bangladesh" & Year==1996

gen Type_Election="Parliamentary"

sort Country Year Month
order Country Year Month Type_Election flag_two_round flag_constituent
format Country %20s
compress
label data ""

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_two_round   "Flags elections with two rounds"
lab var flag_constituent  "Flags consituent assembly election"

save "$project_path/data/2_intermediary/elections/Parliamentary elections/parl_elec_vdem", replace

